var registroActual=null;
var winCargarPlantilla = null;
function tipoDocumento(){
    var dsTiposDocumento=new Ext.data.Store({
        autoDestroy: true,
        url: "lista-tipos-documento",
        reader: new Ext.data.JsonReader(
        {
            root: "tipos",
            fields:
            [
            {
                name: "ntidoIdentificador",
                type: "integer"
            },
            {
                name: "dtidoFechaCreacion"
            },
            {
                name: "ctidoDescripcion"
            }
            ]
        })
    });
    dsTiposDocumento.on("exception",function(proxy, type, action, options, response, arg){
        Ext.MessageBox.alert("Error",response.responseText);
    });
    var grdTiposDocumento=new Ext.grid.GridPanel({
        title: "Tipos de Documentos",
        store: dsTiposDocumento,
        loadMask: false,
        stripeRows: true,
        height:400,
        columns:[new Ext.grid.RowNumberer(),{
            header: "Descripción",
            dataIndex: "ctidoDescripcion",
            width: 500
        }],
        tbar:[{
            text:"Nuevo",
            iconCls: "icon-add",
            handler: function(){
                nuevoTipoDocumento();
            }
        },"-",{
            text: "Eliminar",
            iconCls: "icon-delete",
            handler: function(){
                eliminarTipoDocumento();
            }
        },"-",{
            text: "Cargar Plantilla",
            iconCls: "icon-sincronize",
            handler: function(){
                cargarPlantilla();
            }
        },"-",{
            text: "Ver plantilla",
            iconCls:"icon-preview",
            handler: function() {
                verPlantilla();
            }
        },"-",{
            text: "Imprimir",
            iconCls:"icon-print",
            handler: function() {
                printTipoDocumento();
            }
        }]
    });


    grdTiposDocumento.on("render",function(component){
        dsTiposDocumento.load();
    });
    var seleccionTipoDocumento=grdTiposDocumento.getSelectionModel();
    seleccionTipoDocumento.on("rowselect",function(sm,fila,registro){
        registroActual=registro;
    });
    var win = new Ext.Window({
        title:'Mantenimiento de Tipo de Documento',
        closeAction:'destroy',
        width:600,
        height:550,
        x:0,
        y:0,
        shim:false,
        maximizable:false,
        resizable: false,
        animCollapse:false,
        layout: 'fit',
        items:grdTiposDocumento
    });
    win.show();
}

function printTipoDocumento(){
    var width  = screen.width-50;
    var height = screen.height-100;
    var left   = 10;
    var top    = 10;
    var params = 'width='+width+',height='+height;
    params += ',top='+top+',left='+left;
    params += ',directories=no';
    params += ',location=no';
    params += ',menubar=no';
    params += ',resizable=yes';
    params += ',scrollbars=no';
    params += ',status=no';
    params += ',toolbar=no';
    window.open("TipoDocumentoServlet.pdf",'windowname',params);
}


function cargarPlantilla(){
    var frmCargarPlantilla=new Ext.Panel({
        bodyBorder:true,
        height:180,
        bodyStyle: "background-color:#DFE8F6; border:0"
    });
    new Ajax.Request("frmCargarPlantilla.jsp",{
        method: "post",
        asynchronous:false,
        onSuccess: function(transport){
            frmCargarPlantilla .html=transport.responseText;
            winCargarPlantilla = new Ext.Window({
                title:'Gestionar Dictamen',
                closeAction:'destroy',
                width:600,
                height: 300,
                shim:false,
                maximizable:false,
                modal:true,
                resizable: false,
                animCollapse:false,
                layout: 'fit',
                items:frmCargarPlantilla,
                bbar:[{
                    text:'Grabar',
                    iconCls:'icon-save',
                    handler: function(){
                        
                    }
                },'-',{
                    text:'Cancelar',
                    iconCls:'icon-cancel',
                    handler: function(){
                        winCargarPlantilla.destroy();
                    }
                }]
            });
            winCargarPlantilla.show();
            $("ntidoid").value = registroActual.data.ntidoIdentificador;
        }
    });
}